package org.budo.support.logback;

import java.nio.charset.StandardCharsets;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;

/**
 * @author lmw
 */
public class Logback {
    /**
     * 生成一个时间滚动日志
     * 
     * @param name       日志名称
     * @param addConsole 是否添加到控制台
     * @return 日志者
     */
    public static org.slf4j.Logger createTimeBasedRollingFileLogger(String name, boolean addConsole) {
        String fnp = "/taojin/log/tj-wsdc-connector-web" + "/" + name + "-%d{yyyy-MM-dd}.log";
        String file = "/taojin/log/tj-wsdc-connector-web" + "/" + name + ".log";
        String pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p [%t] [%X{requestId}] %-40.40logger{39} %L : %m%n -%wEx";

        Logger templateLogger = (Logger) LoggerFactory.getLogger(Logback.class);
        LoggerContext loggerContext = templateLogger.getLoggerContext();

        RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();

        PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
        patternLayoutEncoder.setContext(loggerContext);
        patternLayoutEncoder.setPattern(pattern);
        patternLayoutEncoder.setCharset(StandardCharsets.UTF_8);
        patternLayoutEncoder.start();

        TimeBasedRollingPolicy<?> timeBasedRollingPolicy = new TimeBasedRollingPolicy();
        timeBasedRollingPolicy.setContext(loggerContext);
        timeBasedRollingPolicy.setFileNamePattern(fnp);
        timeBasedRollingPolicy.setMaxHistory(30);
        timeBasedRollingPolicy.setParent(rollingFileAppender);
        timeBasedRollingPolicy.start();

        ThresholdFilter thresholdFilter = new ThresholdFilter();
        thresholdFilter.setContext(loggerContext);
        thresholdFilter.setLevel(Level.INFO.levelStr);
        thresholdFilter.start();

        rollingFileAppender.setContext(loggerContext);
        rollingFileAppender.setFile(file);
        rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);
        rollingFileAppender.setName(name);
        rollingFileAppender.setEncoder(patternLayoutEncoder);
        rollingFileAppender.addFilter(thresholdFilter);
        rollingFileAppender.start();

        Logger logger = loggerContext.getLogger(name);
        logger.addAppender(rollingFileAppender);

        logger.setAdditive(false);

        if (addConsole) {
            Appender<ILoggingEvent> console = loggerContext.getLogger("root").getAppender("CONSOLE");
            logger.addAppender(console);
        }

        return logger;
    }
}